package top.scsoul.pas.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import top.scsoul.pas.entity.User;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findByLoginName(String loginName);

    @Query(value = "select distinctrow sys_user.* from sys_user,sys_user_role where sys_user_role.role_id = 2", nativeQuery = true)
    List<User> getTachers();

    @Query(value = "select count(sys_user.user_id) from sys_user where user_login_name = ?1", nativeQuery = true)
    long getCountByLoginName(String loginName);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update sys_user set sys_user.user_state = ?2 where user_id=?1", nativeQuery = true)
    void changeState(Long id, String state);


}
